<script setup lang="ts">
import { requiredCheck, singleIpCheck, validatePortCheck } from "@/utils/verify";
import { nextTick, ref } from "vue";
import { addData, editData } from "@/api/modules/resource/sample-finger";
import SelectAppInput from "@/components/SelectAppInput/index.vue";
import SelectFingerTextData from "@/components/SelectFingerTextData/index.vue";
import SelectFingerHostData from "@/components/SelectFingerHostData/index.vue";
import SelectHserverData from "@/components/SelectHserverData/index.vue";

interface Props {
  title?: string;
  defaultSelectFinger?: string;
  defaultSelectSrvid?: string;
  rowData?: Object;
}

const props = withDefaults(defineProps<Props>(), {
  title: "请选择虚拟资源",
  rowData: undefined,
  defaultSelectFinger: "",
  defaultSelectSrvid: ""
});
defineOptions({
  name: "UpdateForm" // 默认策略
});
import useUpdate from "@/hooks/useUpdate";
import { ifaceAll, ifacemodeUpdate } from "@/api/modules/dynamic-camouflage/line-config";
import SelectLinksInput from "@/components/SelectLinksInput/index.vue";
import { scfgBatchFing, scfgBatchHserver } from "@/api/modules/dynamic-camouflage/sentinel-config";
import { ElMessage } from "element-plus";

const emits = defineEmits(["success"]);
const { updateFormData, updateFormRef, updateFormRules, submit, showHandle, openHandle, closeHandle } = useUpdate({
  rules: {
    name: [...requiredCheck("请输入指纹名称")],
    app: [...requiredCheck("请选择协议")],
    port: [...requiredCheck("请输入端口"), { validator: validatePortCheck }],
    text: [...requiredCheck("请输入关键信息")]
  },
  defaultData: {
    type: 1
  }
});

async function selectHs(data, id) {
  console.log(data);
  const hs = data.map(item => item.id);
  updateFormData.hs = hs.join();
}

async function selectFinger(data, id) {
  console.log(data);
  const fingerips = data.rowList.map(item => item.ip);
  updateFormData.fingerips = fingerips.join();
}

async function submitCb() {
  // 仅更新指纹
  if (updateFormData.type === 1) {
    console.log("props.rowData", props.rowData);
    await scfgBatchFing({
      id: props.rowData.id,
      fingerip: updateFormData.fingerips,
      ifid: props.rowData.ifid,
      interval: 0
    });
    await scfgBatchHserver({
      id: props.rowData.id,
      srvid: "",
      ifid: props.rowData.ifid
    });
  }
  // 仅更新高交
  if (updateFormData.type === 2) {
    await scfgBatchFing({
      id: props.rowData.id,
      fingerip: "",
      ifid: props.rowData.ifid,
      interval: 0
    });
    await scfgBatchHserver({
      id: props.rowData.id,
      srvid: updateFormData.hs,
      ifid: props.rowData.ifid
    });
  }
  ElMessage.success("更新成功");
  emits("success");
  showHandle.value = false;
}

function openHandleCb() {
  openHandle({
    row: {
      ...props.rowData
    },
    id: props.rowData?.id
  });
  nextTick(() => {
    console.log(props.defaultSelectFinger);
    console.log(props.defaultSelectSrvid);
    if (props.defaultSelectFinger) {
      updateFormData.type = 1;
    }
    if (props.defaultSelectSrvid) {
      updateFormData.type = 2;
    }
    console.log(updateFormData.type);
  });
}
</script>

<template>
  <div class="update-form mg-r-sm" style="display: inline-block" @click="openHandleCb">
    <slot>
      <el-button type="primary">新增</el-button>
    </slot>
  </div>
  <base-dialog v-if="showHandle" @confirm="submitCb()" width="1000px" :title="title" v-model="showHandle">
    <div class="details">
      <div class="details-main">
        <el-form ref="updateFormRef" :model="updateFormData" :rules="updateFormRules" label-width="50px" label-suffix="：">
          <el-form-item prop="type">
            <el-radio-group v-model="updateFormData.type">
              <el-radio :value="1">主机指纹</el-radio>
              <el-radio :value="2">虚拟服务</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-divider style="width: 93%; margin: auto"></el-divider>
          <div style="padding: 10px 0px 30px 30px" v-if="updateFormData.type == 1">
            <!--            :defaultSelect="data.row.fingerip"-->
            <!--            @select="d => selectFinger(d, data.row.id)"-->
            <SelectFingerHostData
              @select="d => selectFinger(d, props.rowData.id)"
              :noDialog="true"
              :defaultSelect="defaultSelectFinger"
              :maxMultipleNum="1"
            >
              <el-icon class="cursor-pointer">
                <Document />
              </el-icon>
            </SelectFingerHostData>
          </div>
          <div style="padding: 10px 0px 30px 30px" v-if="updateFormData.type == 2">
            <!--            :defaultSelect="data.row.srvid" @select="d => selectHs(d, data.row.id)"-->
            <SelectHserverData @select="d => selectHs(d, props.rowData.id)" :noDialog="true" :defaultSelect="defaultSelectSrvid">
              <el-icon class="cursor-pointer">
                <Document />
              </el-icon>
            </SelectHserverData>
          </div>
        </el-form>
      </div>
    </div>
    <template #footer-more-button></template>
  </base-dialog>
</template>

<style lang="scss" scoped></style>
